package hyrpt

class BadProcessAnalysisController {
    def dynamicQueryService
    def badProcessAnalysis(){
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def tableHeadList=[]
        def badProcessAnalysis=[]
        def badProcessAnalysisChart = []
        def pageSizeList = [10,20,50,100]
        def totalCount =0
        def badProcessAnalysisSumQty=[]
        List<Integer> list=new ArrayList<Integer>();
        if(params.EXPORT_CONTENT_NAME=='Excel'){
            params.SYS_QUERY_NAME = 'badProcessAnalysis'
            badProcessAnalysis = dynamicQueryService.queryForList(params)
            if (badProcessAnalysis.size() > 0) {
                tableHeadList = badProcessAnalysis[0].keySet()
            }

            params.SYS_QUERY_NAME = 'badProcessAnalysisChart'
            badProcessAnalysisChart = dynamicQueryService.queryForList(params)

            params.SYS_QUERY_NAME = 'badProcessAnalysisSumQty'
            badProcessAnalysisSumQty = dynamicQueryService.queryForList(params)
        }
        [startno:params.startno?:'',endno:params.endno?:'',pageSize     : pageSize, totalCount: totalCount, pageSizeList: pageSizeList,pageCurrent:pageCurrent,
         tableHeadList:tableHeadList,badProcessAnalysis:badProcessAnalysis,badProcessAnalysisChart:badProcessAnalysisChart,badProcessAnalysisSumQty:badProcessAnalysisSumQty,
         startTime:params.startTime,endTime:params.endTime,partName:params.partName,stepName:params.stepName,part:params.part?:'',batchid:params.batchid?:'',defectcode:params.defectcode?:'',lotId:params.lotId?:'',
         stepDesc:params.stepDesc,group4:params.group4,partDesc:params.partDesc,defectType:params.defectType,checkStep:params.checkStep,
         totalGroup:params.totalGroup,detailCate:params.detailCate,list:list]

    }

    def exportExcel() {
        def sysQueryName


        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999



        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
                tableHeadList = dataList[0].keySet()
        }

        print(tableHeadList)

        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
    }
}